var mygrid = {};
var users = {};
$(function(){
	mygrid = $("#list").jqGrid({
		url : "admin/preuser/pre-user!datalist",
		datatype : "json",
		mtype : 'GET',
		colNames : ['选择','注册名','中文名','部门','角色','注册日期','管理'],
		colModel : [
		{			
			name : 'username',
			index : 'username',
			width : 30,
			formatter:selectFormatter
		},			
	    {
			name:'username',
			index:'username',
			width:80
	    },
	    {
	    	name:'localName',
	    	index:'localName',
	    	width:80
	    },
		{
			name:'department',
			index:'department',
			width:100,
			formatter:nameFormatter
		},{
			name : 'role',
			index : 'role',
			width : 100 ,
			formatter:nameFormatter
		},{
			name : 'requestDate',
			index : 'requestDate',
			width : 100,	
		},{
			name : 'username',
			index : 'username',
			width : 30,
			formatter:passedFormatter
		}],
		pager : '#pager',
		rowNum : 10,
		rowList : [ 10, 20, 30 ],
		sortname : 'requestDate',
		sortorder : 'desc',
		viewrecords : true,
		caption : '待审核用户列表',
		width : '750',
		height : '300',
		jsonReader : {
			repeatitems : false,
			id : "0"
		}
	});
	$("button.passall").bind("click",function(){
		if(!confirm("要审核通过这些用户?"))return;
		if($.isEmptyObject(users)) {
			alert("请选择用户");
			return;
		}
		var params = {};
		var i=0;
		for(name in users){
			if(users[name]){
				var pname="beans["+i+"].username";
				var pvalue=name;
				params[pname]=pvalue;
			}			
		}		
		$.post("admin/preuser/pre-user!passall",params,function(html){
			$("#errorMsg").empty();
			$("#errorMsg").html(html);
			mygrid.trigger("reloadGrid");
		});
		users = {};
	});
});
function nameFormatter(v,opt,row){
	return v?v['name']:'-';
}
function passedFormatter(v,opt,row){
	var alink = $("<a href=\"javascript:;\" onclick=\"passTheUser(this)\"  user=\""+v+"\">通过</a>");
	return $("<p></p>").append(alink).html();
}
function selectFormatter(v,opt,row){
	var chk = $("<input type=\"checkbox\" onclick=\"checkuser(this);\" value=\""+v+"\"></inpupt>");
	return $("<p></p>").append(chk).html();
}
function passTheUser(u){
	var params = {"beans[0].username":u.user};
	$.post("admin/preuser/pre-user!passall",params,function(html){
		$("#errorMsg").empty();
	    createNotify("用户"+u.user+"已通过!");
		mygrid.trigger("reloadGrid");
	});
	users = {};
}
function checkuser(obj){
	users[obj.value] = obj.checked;	
}